﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Personnel
{
    public class PersonnelNominees : DetailView<personnelnominee>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<personnelnominee> EntityList;
        private new personnelnominee Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<personnelnominee>

        public override List<personnelnominee> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.personnelnominees.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<personnelnominee> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.personnelnominees.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override personnelnominee GetEntity()
        {
            _context = new project5188Entities();
            Entity = new personnelnominee();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTopersonnelnominees(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<personnelnominee> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText, DateTime? startWorkDate, string workProvince, string workCompanyName, bool? isStartToWork)
        {
            _context = new project5188Entities();

            int registryNo = 0, tcNo = 0;

            if (!String.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                Int32.TryParse(registryNoText, out registryNo);
            }

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                Int32.TryParse(tcNoText, out tcNo);
            }

            var objectParameters = new ObjectParameter[7];

            var registryParameter = new ObjectParameter("RegistryNo", typeof(int)) { Value = registryNo };
            objectParameters[0] = registryParameter;

            var nameSurnameParameter = new ObjectParameter("NameSurname", typeof(string)) { Value = nameSurname };
            objectParameters[1] = nameSurnameParameter;

            var tcNoParameter = new ObjectParameter("TCNo", typeof(int)) { Value = tcNo };
            objectParameters[2] = tcNoParameter;

            var startWorkDateParameter = new ObjectParameter("StartWorkDate", typeof(DateTime?)) { Value = startWorkDate };
            objectParameters[3] = startWorkDateParameter;

            var workProvinceParameter = new ObjectParameter("WorkProvince", typeof(string)) { Value = workProvince };
            objectParameters[4] = workProvinceParameter;

            var workCompanyNameParameter = new ObjectParameter("WorkCompanyName", typeof(string)) { Value = workCompanyName };
            objectParameters[5] = workCompanyNameParameter;

            var isStartToWorkParameter = new ObjectParameter("IsStartToWork", typeof(bool?)) { Value = isStartToWork };
            objectParameters[6] = isStartToWorkParameter;

            var list = _context.ExecuteFunction<personnelnominee>("GetPersonnelNomineeListByParameters", objectParameters).ToList();

            return list;
        }

        public List<GetPersonnelNomineeReport_Result> GetPersonnelNomineeReport()
        {
            _context = new project5188Entities();

            return _context.ExecuteFunction<GetPersonnelNomineeReport_Result>("GetPersonnelNomineeReport").ToList();
        }
    }
}
